<template>
    <div>
        <hd-tab :tabs="tabs"></hd-tab>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>发布二手信息</span>
            </div>
            <el-form :model="article" label-width="100px">
                <div v-if="article">
                    <div v-for="(field, index) in article.fields" :key="index">
                        <el-form-item :label="field.title" v-if="field.show">
                            <component v-bind:is="`field-${field.type}`" :field="field" :form="form"
                                       :alert="alert"></component>

                            <!--                            -->
                            <!--  【最终目标是提交form】
                            动态渲染组件，type的值是inut、textarea、editor等-->
                            <!-- :name="field.name"为自定义的字段,如author，url,
                            子组件中用v-model="form[field.name]"，初始状态form={}为空-->
                            <!--     被渲染的组件为form[field.type]-->
                            <hd-error :name="field.name"/>
                        </el-form-item>
                    </div>
                </div>

            </el-form>
            <el-button type="success" @click="onSubmit">保存提交</el-button>
        </el-card>
    </div>
</template>

<script>
import tabs from './tabs'

const form = {}
export default {
    route: false,
    props: ['id'],
    data() {
        return {
            tabs,
            form: {},
            article: {},
            alert: {title: "正方形图片最好", type: 'success'}
        }
    },

    async created() {
        //获取当前模型
        this.article = await this.axios.get(`admin/article/6`)
        //获取当前ad的数据
        if (this.id) {
            this.form = await this.axios.get(`admin/article/6/ad/${this.id}`)
        }
    },
    methods: {
        async onSubmit() {
            this.form = Object.assign(this.form, {article_id: 6})
            const url = this.id ? `admin/article/6/ad/${this.id}` : `admin/article/6/ad`;
            await this.axios[this.id ? 'put' : 'post'](url, this.form)
            // this.$router.push({name: 'admin.ad.index'})
        }
    }
}
</script>

<style scoped>

</style>
